<?php
/*APP版本升级模块*/
namespace YMApi\Controller;
use Think\Controller;

class VersionController extends Controller{

    /**
     * @var
     */
    protected $application;

    /**
     * @var
     */
    protected $terminal;


    /**
     * Created by Jerry
     */
    public function _initialize()
    {
        $this->application = M('application_vesion');
        $this->terminal = M('application_terminal');
    }


    /**
     * Created by Jerry
     */
    public function update(){
        $terminalWhere = array(
            'name'  =>  '配送端',
        );
        $terminalId = M('application_terminal')->where($terminalWhere)->find();

        $dataWhere = array(
            'terminal'  =>  $terminalId['id'],
        );
        $terminal = $this->application->where($dataWhere)->order('id desc')->find();

        if (empty($terminal)) {
            returnApiError('暂无更新');
        }

        $data = array(
            'version'   =>  $terminal['version'],
            'url'       =>  'https://oss.shidexian.com/' . $terminal['url'],
            'content'   =>  $terminal['content'],
        );

        returnApiSuccess($data);
    }

    /**
     * HD端应用更新 （（老版本无法使用https更新应用所以要使用http来下载应用包））
     * Created by Jerry
     */
    public function sdx_hd_update(){
        $terminalWhere = array(
            'name'  =>  'HD',
        );
        $terminalId = M('application_terminal')->where($terminalWhere)->find();

        $dataWhere = array(
            'terminal'  =>  $terminalId['id'],
        );
        $terminal = $this->application->where($dataWhere)->order('id desc')->find();

        if (empty($terminal)) {
            returnApiError('暂无更新');
        }

        $data = array(
            'version'   =>  $terminal['version'],
            'url'       =>  'http://oss.shidexian.com/' . $terminal['url'],
            'content'   =>  $terminal['content'],
        );

        returnApiSuccess($data);
    }

    /**
     * 获取app端更新 （老版本无法使用https更新应用所以要使用http来下载应用包）
     * Created by Jerry
     */
    public function sdx_app_update(){
        $terminalWhere = array(
            'name'  =>  'APP',
        );
        //何锐修改
        //1、为活动图标 =2 为用普通图标 =3、不启用判断
        if(I("post.version") >= 26){
            $stretch = 1;
        }else{
            $stretch = 2;
        }
        //何锐修改
		$stretch = 2;
        $terminalId = M('application_terminal')->where($terminalWhere)->find();

        $startTime = strtotime('2016-12-12 00:00');
        $endTime = strtotime('2016-12-13  23:59:00');

        //$stretch = 3;

//        if (time() > $startTime && time() < $endTime) {
//			$stretch = 2;
//		}

        $dataWhere = array(
            'terminal'  =>  $terminalId['id'],
        );
        $terminal = $this->application->where($dataWhere)->order('id desc')->find();

        if (empty($terminal)) {
			$res['stretch'] = $stretch;
            returnApiError('暂无更新', $res);
        }

        $data = array(
            'version'   =>  $terminal['version'],
            'url'       =>  'http://oss.shidexian.com/' . $terminal['url'],
            'content'   =>  $terminal['content'],
            'stretch'   =>  $stretch,
        );

        returnApiSuccess($data);

    }


	/**
	 *首次获取appID 和access
	 * @return bool
     */
	public function get_app_id(){
		if(is_wechat_browser()){
			$date['app_id'] = C("H5_APP_ID");
		}elseif(is_android()){
			$date['app_id'] = C("ANDROID_APP_ID");
		}elseif(is_ios()){
			$date['app_id'] = C("IOS_APP_ID");
		}else{
			returnApiError("无法识别的客户端");
		}

		$date['access'] = $this->produce_string();
		S("access",$date['access'],600);
		returnApiSuccess($date);
	}

	/**
	 *获取token
     */
	public function get_token(){
		$access_s = S("access");
		$app_id = I("post.app_id");
		$access_c = I("post.access");
		if($access_s == $access_c){
			$date['token'] = $this->produce_token($app_id,$access_c);
			returnApiSuccess($date);
		}else{
			returnApiError("access 错误");
		}
	}


	/**
	 * 生成一个10位数的随机字符串
	 * @return mixed
     */
	private function produce_token($app_id,$access_c){

		$str=md5($app_id.$access_c.time());
		S($app_id,$str,600);
		return $str;
	}

	private function produce_string(){
		$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
		$chars   =   str_shuffle($chars);
		$str_name     =   substr($chars,0,10);
		$str_vaule=md5($str_name.time());
		//S($str_vaule,true,600);	//有效缓存时间 600秒  10分钟
		return $str_name;
	}
    
    
}